package services

import (
	"log"

	"github.com/deploy-services/v2/kcfg"
)

type env_type struct {
	name  string
	type_ string
	value string
}

type environment struct {
	name string
	envs []env_type
}

func parseEnvironment(n *kcfg.Node) *environment {
	sv := &environment{
		envs: []env_type{},
	}
	sv.name = n.Child("name").GetString()
	envs_keys, envs_nodes := n.Child("env").ChildsAllWithoutNote()
	for i, name := range envs_keys {
		env := env_type{}
		env.name = name
		env.type_ = envs_nodes[i].Child("type").GetString()
		env.value = envs_nodes[i].Child("value").GetString()
		sv.envs = append(sv.envs, env)
	}
	return sv
}

type environments map[string]*environment

func parseEnvironments(n []*kcfg.Node) environments {
	rest := make(environments)
	for _, nn := range n {
		sv := parseEnvironment(nn)
		if _, ok := rest[sv.name]; ok {
			log.Println("Warn", sv.name, " environment repeat,will ignore last!!!")
		}
		rest[sv.name] = sv
	}
	return rest
}
